from pyecharts.charts import Page
import pandas as pd
import sys
sys.path.append("..//database")
from SqlWizard import *
from CmnFuncs import *

def run_metal():
    '''
    本模块包含品种：沪金、沪银、铜、镍、铝、锌、锡、铅
    设计图表：
    # 1. 沪金现货基差图
    # 2. 沪银现货基差图
    # 3. 金银现货比价， 主连比价
    # 4. 铜.....
    # 5. 镍.....
    # 6. 铝
    # 7. 锌
    # 8. 锡
    # 9. 铅
    '''

    au_spot = read_sql_spot('spot_au')
    au_index = read_sql_fut_index('fut_index_au')
    au_ry = read_sql_ry('roll_yield_au')
    au_spdrdx = read_sql_spdrdx('spdrdex_au')
    ag_spot = read_sql_spot('spot_ag')
    ag_index = read_sql_fut_index('fut_index_ag')
    ag_ry = read_sql_ry('roll_yield_ag')
    ag_spdrdx = read_sql_spdrdx('spdrdex_ag')
    ni_spot = read_sql_spot('spot_ni')
    ni_index = read_sql_fut_index('fut_index_ni')
    ni_ry = read_sql_ry('roll_yield_ni')
    ni_spdrdx = read_sql_spdrdx('spdrdex_ni')
    cu_spot = read_sql_spot('spot_cu')
    cu_index = read_sql_fut_index('fut_index_cu')
    cu_ry = read_sql_ry('roll_yield_cu')
    cu_spdrdx = read_sql_spdrdx('spdrdex_cu')
    al_spot = read_sql_spot('spot_al')
    al_index = read_sql_fut_index('fut_index_al')
    al_ry = read_sql_ry('roll_yield_al')
    al_spdrdx = read_sql_spdrdx('spdrdex_al')
    zn_spot = read_sql_spot('spot_zn')
    zn_index = read_sql_fut_index('fut_index_zn')
    zn_ry = read_sql_ry('roll_yield_zn')
    zn_spdrdx = read_sql_spdrdx('spdrdex_zn')
    sn_spot = read_sql_spot('spot_sn')
    sn_index = read_sql_fut_index('fut_index_sn')
    sn_ry = read_sql_ry('roll_yield_sn')
    sn_spdrdx = read_sql_spdrdx('spdrdex_sn')
    pb_spot = read_sql_spot('spot_pb')
    pb_index = read_sql_fut_index('fut_index_pb')
    pb_ry = read_sql_ry('roll_yield_pb')
    pb_spdrdx = read_sql_spdrdx('spdrdex_pb')

    header_metals = draw_separator('贵金属及有色')
    # 1. 沪金现货基差图
    au_spot_chart = draw_spot_spd('沪金', au_spot)
    # 沪金一致性预期指标
    idctr_au = draw_spec_idctr('沪金', au_spdrdx, au_index, au_ry)
    # 2. 沪银现货基差图
    ag_spot_chart = draw_spot_spd('沪银', ag_spot, 1)
    # 沪银一致性预期指标
    idctr_ag = draw_spec_idctr('沪银', ag_spdrdx, ag_index, ag_ry)
    # 4. 铜.....
    cu_spot_chart = draw_spot_spd('铜', cu_spot, 3)
    # 铜一致性预期指标
    idctr_cu = draw_spec_idctr('铜', cu_spdrdx, cu_index, cu_ry)
    # 5. 镍.....
    ni_spot_chart = draw_spot_spd('镍', ni_spot, 4)
    # 镍一致性预期指标
    idctr_ni = draw_spec_idctr('镍', ni_spdrdx, ni_index, ni_ry)
    # 6. 铝
    al_spot_chart = draw_spot_spd('铝', al_spot, 5)
    # 铝一致性预期指标
    idctr_al = draw_spec_idctr('铝', al_spdrdx, al_index, al_ry)
    # 7. 锌
    zn_spot_chart = draw_spot_spd('锌', zn_spot)
    # 锌一致性预期指标
    idctr_zn = draw_spec_idctr('锌', zn_spdrdx, zn_index, zn_ry)
    # 8. 锡
    sn_spot_chart = draw_spot_spd('锡', sn_spot, 1)
    # 锡一致性预期指标
    idctr_sn = draw_spec_idctr('锡', sn_spdrdx, sn_index, sn_ry)
    # 9. 铅
    pb_spot_chart = draw_spot_spd('铅', pb_spot, 2)
    # 铅一致性预期指标
    idctr_pb = draw_spec_idctr('铅', pb_spdrdx, pb_index, pb_ry)

    # 品种间
    header_cross = draw_separator('品种间')
    # 3. 金银现货比价， 主连比价
    arbi_record('au', au_index['close'], 'ag', ag_index['close'], x=1, y=1, opt=1, cat=1)
    arbi_record('au', au_spot['spot'], 'ag', ag_spot['spot'], x=1, y=1, opt=1, cat=2)
    ratio_au_ag_spot = draw_price_ratio('黄金现货', au_spot['spot'], '白银现货', ag_spot['spot'], 2)
    ratio_au_ag_main = draw_price_ratio('黄金主连', au_spot['main_price'], '白银主连', ag_spot['main_price'], 2)

    page = Page(page_title='贵金属及有色金属', layout=Page.SimplePageLayout)
    page.add(
        header_metals,
        au_spot_chart,
        idctr_au,
        ag_spot_chart,
        idctr_ag,
        cu_spot_chart,
        idctr_cu,
        ni_spot_chart,
        idctr_ni,
        al_spot_chart,
        idctr_al,
        zn_spot_chart,
        idctr_zn,
        sn_spot_chart,
        idctr_sn,
        pb_spot_chart,
        idctr_pb,
        header_cross,
        ratio_au_ag_spot,
        ratio_au_ag_main
    )
    page.render('贵金属及有色金属.html')